/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import com.bourre.data.collections.Map;

import fever.Fever;
import fever.log.FeverDebug;
import fever.utils.Stringifier;

/**
 * Control and manage browser popup window.
 *
 * @author Romain Ecarnot
 */
class fever.app.browser.BrowserPopup
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private static var _m : Map = new Map();
	
	private var _id : String;	
	private var _url : String;	private var _toolbar : Boolean;	private var _locationbar : Boolean;	private var _directories : Boolean;	private var _status : Boolean;	private var _menubar : Boolean;	private var _scrollbars : Boolean;	private var _resizable : Boolean;	private var _width : Number;	private var _height : Number;	private var _x : Number;	private var _y : Number;
	
	private var _isOpened : Boolean;
	
	
	//-------------------------------------------------------------------------
	// Public Properties
	//-------------------------------------------------------------------------
	
	/** Popup's position on X-Axis. */
	public function get x() : Number { return _x; }
	public function set x( n : Number ) { move( n, null ); }
	
	/** Popup's position on Y-Axis. */
	public function get y() : Number { return _y; }
	public function set y( n : Number ) { move( null, n ); }
	
	/** Popup's width. */
	public function get width() : Number { return _width; }
	public function set width( w : Number ) { setSize( w, null ); }
	
	/** Popup's height. */
	public function get height() : Number { return _height; }
	public function set height( h : Number ) { setSize( null, h ); }
	
	/** Enabled scrollbars. */
	public function get scrollbars() : Boolean { return _scrollbars; }
	public function set scrollbars( b : Boolean ) { _scrollbars = b; }
	
	/** Enabled resizing. */
	public function get resizable() : Boolean { return _resizable; }
	public function set resizable( b : Boolean ) { _resizable = b; }
	
		
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Returns the popup registred with {@code winID} name.
	 * 
	 * <p>If <code>winID</code> does not exist, it will be created.
	 */
	public static function get( winID : String ) : BrowserPopup
	{
		if( _m.containsKey( winID ) ) return _m.get( winID );
		else return new BrowserPopup( winID );
	}
	
	/**
	 * Returns window's id.
	 */
	public function getID(  ) : String
	{
		return _id;	
	}
	
	/**
	 * Sets popup content.
	 */
	public function setURL( url : String ) : Void
	{
		if( url ) _url = url;
		
		if( _isOpened )
		{
			Fever.application.openURL( 
				"javascript:" + 
				_getWinName() + ".opener.location = '" + _url + "'" 
			);
		}
	}
	
	/**
	 * Returns correct popup content.
	 */
	public function getURL( ) : String
	{
		return _url;	
	}
	
	/**
	 * Opens popup and load passed-in {@code url}
	 */
	public function open() : Void
	{
		_openPopup( );
	}
	
	/**
	 * Closes the popup.
	 * 
	 * <p>Stay registred in manager.
	 */
	public function close() : Void
	{
		_closePopup();
	}
	
	/**
	 * Disposes popup registred with {@code winID} name.
	 * 
	 * <p>Closes the popup and unregisters it from {@link BrowserPopup} 
	 * manager.
	 */
	public function dispose( winID : String ) : Void
	{
		if( _m.containsKey( winID ) ) 
		{
			_m.get( winID ).close();
			_m.remove( winID );
		}
		else FeverDebug.ERROR( winID + ' popup is not registred' );
	}
	
	/**
	 * Moves the popup to {@code x, y} coordinates.
	 * 
	 * <p>If popup location ( url ) is not in the same domain that your 
	 * main application, action can be denied.
	 */
	public function move( x : Number, y : Number ) : Void
	{
		if( x ) _x = x;
		if( y ) _y = y;
		
		if( _isOpened )
		{
			Fever.application.openURL( 
				"javascript:" + 
				_getWinName() + ".moveTo( " + _x + ", " + _y + " );" + 
				_getWinName() + ".focus();" 
			);
		}
	}
	
	/**
	 * Resizes the popup to {@code width}x{@code height} size.
	 * 
	 * <p>If popup location ( url ) is not in the same domain that your 
	 * main application, action can be denied.
	 */
	public function setSize( width : Number, height : Number ) : Void
	{
		if( width ) _width = width;
		if( height ) _height = height;
		
		if( _isOpened )
		{
			Fever.application.openURL( 
				"javascript:" + 
				_getWinName() + ".resizeTo( " + _width + ", " + _height + " );" + 
				_getWinName() + ".focus();" 
			);
		}
	} 
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		return Stringifier.parse( this );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	private function BrowserPopup( winID : String )
	{
		_id = winID;
		
		_toolbar = false;
		_locationbar = false;
		_directories = false;
		_status = false;
		_menubar = false;
		_scrollbars = true;
		_resizable = false;
		_width = 400;
		_height = 300;
		_x = 10;
		_y = 10;
		
		_isOpened = false;
		
		_m.put( _id, this );
	}
	
	private function _getWinName() : String
	{
		return 'win' + _id;	
	}
	
	private function _boolToString( b : Boolean ) : String
	{
		return ( b ) ? 'yes' : 'no';	
	}
	
	private function _openPopup( ) : Void
	{
		Fever.application.openURL( "javascript:" + _getWinName() + " = window.open(\'" 
			+ _url 
			+ "\',\'" 
			+ _id
			+ "\',\'toolbar=" + _boolToString( _toolbar )
			+ ",location=" + _boolToString( _locationbar )
			+ ",directories=" + _boolToString( _directories )
			+ ",status=" + _boolToString( _status )
			+ ",menubar=" + _boolToString( _menubar )
			+ ",scrollbars=" + _boolToString( _scrollbars )
			+ ",resizable=" + _boolToString( _resizable )
			+ ",width=" + ( _width + 18 ) 
			+ ",height=" + ( _height + 18 ) 
			+ ",left=" + _x
			+ ",top=" + _y
			+ "\');void(0);"
			+ _getWinName() + ".focus();", "");
		
		_isOpened = true;
	}
	
	private function _closePopup() : Void
	{
		Fever.application.openURL( "javascript:" + _getWinName() + ".close();");
		
		_isOpened = false;
	}

}